﻿@page "/organization/index"
@inherits AdminCompontentBase

<PageTitle>
    @T("Department Management")
</PageTitle>

<MRow Class="full-height" NoGutters>
    <MCol Md="@("auto")">
        <MCard style="width:300px;max-height: calc(100vh - 120px);" Class="full-height">
            <MCardTitle>
                <SSearch @bind-Value="@_searchName"/>
            </MCardTitle>
            <MCardText Style="overflow-y: scroll;height: calc(100% - 72px);">
                <MTreeview @bind-Active="_departmentActive"
                           Items="_departments"
                           Search="@_searchName"
                           OnActiveUpdate="ActiveUpdated"
                           MandatoryActive Open="_openNode"
                           TItem="DepartmentDto"
                           TKey="Guid"
                           Activatable Hoverable
                           ItemText="r=>r.Name"
                           ItemKey="r=>r.Id"
                           ItemChildren="(r=>r.Children.Count < 1 ? null : r.Children)"
                           ActiveClass="primary white--text rounded-lg">
                    <LabelContent>
                        <MHover Context="hoverComtext">
                            <div @attributes="hoverComtext.Attrs" class="d-flex justify-space-between align-center">
                                <span>@context.Item.Name</span>
                                <MMenu Right Bottom class="mr-2">
                                    <ActivatorContent Context="activatorContext">
                                        <SButton style="@(hoverComtext.Hover?"display:flex":"display:none")" Icon @attributes="@activatorContext.Attrs">
                                            <SIcon Style="@(context.Active?"color:white;":"color:#7681AB;")" Small>mdi-dots-vertical</SIcon>
                                        </SButton>
                                    </ActivatorContent>
                                    <ChildContent>
                                        <MList>
                                            @if (!context.Item.IsRoot)
                                            {
                                                <MListItem OnClick="()=>Copy(context.Item.Id)">
                                                    <SIcon Small IsDefaultToolTip="false">mdi-content-copy</SIcon>
                                                    <MListItemTitle Class="ml-2"> @T("Copy") </MListItemTitle>
                                                </MListItem>
                                            }
                                            <MListItem OnClick="()=> _orgSheet.Update(context.Item.Id)">
                                                <SIcon Small IsDefaultToolTip="false">@IconConstants.Update</SIcon>
                                                <MListItemTitle Class="ml-2"> @T("Edit") </MListItemTitle>
                                            </MListItem>
                                            <MListItem OnClick="()=>_orgSheet.Add(context.Item.Id)">
                                                <SIcon Small IsDefaultToolTip="false">@IconConstants.Add</SIcon>
                                                <MListItemTitle Class="ml-2"> @T("AddChildDepartment") </MListItemTitle>
                                            </MListItem>
                                        </MList>
                                    </ChildContent>
                                </MMenu>
                            </div>
                        </MHover>
                    </LabelContent>
                </MTreeview>
            </MCardText>
        </MCard>
    </MCol>
    <MCol Class="d-flex flex-column ml-6">
        <MRow Class="flex-grow-0">
            <MCol Md="4">
                <MCard Height="112">
                    <MListItem class="fill-height">
                        <MListItemAvatar Height=80 Width=80>
                            <MImage Src="/_content/Masa.Auth.Web.Admin.Rcl/img/org/org_2.svg"></MImage>
                        </MListItemAvatar>
                        <MListItemContent>
                            <MListItemTitle class="h5 regular-secondary--text">@T("Secondary Department")</MListItemTitle>
                            <MListItemSubtitle class="h5 emphasis2--text">@_departmentChildrenCountDto.SecondLevel<span class="subtitle2 ml-1">@T("PCS")</span></MListItemSubtitle>
                        </MListItemContent>
                    </MListItem>
                </MCard>
            </MCol>
            <MCol Md="4">
                <MCard Height="112">
                    <MListItem class="fill-height">
                        <MListItemAvatar Height=80 Width=80>
                            <MImage Src="/_content/Masa.Auth.Web.Admin.Rcl/img/org/org_3.svg"></MImage>
                        </MListItemAvatar>
                        <MListItemContent>
                            <MListItemTitle class="h5 regular-secondary--text">@T("Tertiary Department")</MListItemTitle>
                            <MListItemSubtitle class="h5 emphasis2--text">@_departmentChildrenCountDto.ThirdLevel<span class="subtitle2 ml-1">@T("PCS")</span></MListItemSubtitle>
                        </MListItemContent>
                    </MListItem>
                </MCard>
            </MCol>
            <MCol Md="4">
                <MCard Height="112">
                    <MListItem class="fill-height">
                        <MListItemAvatar Height=80 Width=80>
                            <MImage Src="/_content/Masa.Auth.Web.Admin.Rcl/img/org/org_4.svg"></MImage>
                        </MListItemAvatar>
                        <MListItemContent>
                            <MListItemTitle class="h5 regular-secondary--text">@T("Fourth Department")</MListItemTitle>
                            <MListItemSubtitle class="h5 emphasis2--text">@_departmentChildrenCountDto.FourthLevel<span class="subtitle2 ml-1">@T("PCS")</span></MListItemSubtitle>
                        </MListItemContent>
                    </MListItem>
                </MCard>
            </MCol>
        </MRow>
        <MRow Class="flex-grow-1">
            <MCol Md="12">
                <DefaultCard>
                    <AutoHeight>
                        <HeaderContent>
                            <div class="d-flex mb-6">
                                <SSearch @bind-Value="@_getStaffsDto.Search" OnEnter="EnterSearch" />
                                <MSpacer />
                                <SButton Small OnClick="()=>_addStaff=true" BorderRadiusClass="rounded-lg">
                                    <SIcon Size=18 IsDefaultToolTip="false">@IconConstants.Add</SIcon>
                                    <span class="ml-2 btn">@T("Add")</span>
                                </SButton>
                            </div>
                        </HeaderContent>
                        <AutoHeightContent>
                            <SDataTable
                                Dense
                                Height="@("calc(100vh - 420px)")"
                                Headers="@_headers" 
                                Items="@_paginationStaffs.Items" 
                                TItem="StaffDto">
                                <HeaderColContent Context="header">
                                    <span class="body-bold">@header.Text</span>
                                </HeaderColContent>
                                <ItemColContent>
                                    @switch (context.Header.Value)
                                    {
                                        case nameof(UserDto.Name):
                                            <div>
                                                <MAvatar>
                                                    <MImage Src="@(context.Item.Avatar)" Alt="Jack"></MImage>
                                                </MAvatar>
                                                <span class="ml-2">@context.Item.DisplayName</span>
                                            </div>
                                            break;
                                        case "Action":
                                            <MButton Class="mr-4" Icon OnClick="()=>UpdateStaff(context.Item.Id)">
                                                <SIcon Tooltip="@T("Edit")">@IconConstants.Update</SIcon>
                                            </MButton>
                                            break;
                                        default:
                                            <span>@context.Value</span>
                                            break;
                                    }
                                </ItemColContent>
                            </SDataTable>
                        </AutoHeightContent>
                        <FooterContent>
                            <SPagination 
                                Class="mt-4" 
                                Page="_getStaffsDto.Page" 
                                PageSize=_getStaffsDto.PageSize 
                                Total=_paginationStaffs.Total 
                                PageChanged="PageChangedHandler" 
                                PageSizeChanged="PageSizeChangedHandler" />
                        </FooterContent>
                    </AutoHeight>
                </DefaultCard>            
            </MCol>
        </MRow>
    </MCol>
</MRow>

<OrgSheet @ref=_orgSheet OnDelete="DeleteAsync" OnSubmit="SubmitAsync" Departments=_departments />
<CopyOrgSheet @ref="_copyOrgSheet" Departments=_departments OnSubmit="SubmitAsync" />
<Masa.Auth.Web.Admin.Rcl.Pages.Subjects.Users.AddStaffDialog @bind-Visible=_addStaff OnSubmitSuccess="LoadStaffsAsync" DepartmentId="_departmentActive.FirstOrDefault()" />
<Masa.Auth.Web.Admin.Rcl.Pages.Subjects.Users.UpdateStaffDialog @bind-Visible=_updateStaff StaffId=_currentStaffId OnSubmitSuccess="LoadStaffsAsync" />